package org.xtframe.service.impl;

import java.util.Map;

import org.xtframe.drools.business.BusinessExecutor;
import org.xtframe.drools.vo.BusinessResult;
import org.xtframe.entity.SQL;
import org.xtframe.entity.ServerData;
import org.xtframe.entity.UserData;
import org.xtframe.sql.SqlData;
import org.xtframe.util.StringUtil;
import org.xtframe.vo.CheckVO;

public abstract class BasicExecute {

	public CheckVO check(String sqlID, Map<String, Object> parameters, UserData userData, ServerData serverData) throws Exception{
		CheckVO checkVO = new CheckVO();
		if(StringUtil.isNullStr(sqlID)){
			checkVO.setResult(CheckVO.SQLID_NULL);	// sqlID为空
			return checkVO;
		}
		
		BusinessResult bresult = BusinessExecutor.verifySQLExecutivePower(sqlID);
		if(bresult.getResult() != BusinessResult.SUCCESS){
			if(bresult.getResult() == BusinessResult.LOGIN_AGAIN) {
				checkVO.setResult(CheckVO.LOGIN_AGAIN);	// 需要重新登录
			} else {
				checkVO.setResult(CheckVO.SQLID_NO_RIGHT);	// 没有该sql的执行权限
			}
			return checkVO;
		}
		
		SQL sql = serverData.getSql(sqlID);
		if(sql == null){
			checkVO.setResult(CheckVO.SQLID_INEXISTENCE);	// 该sql不存在
			return checkVO;
		}
		
		//判断数据格式是否符合要求
		SqlData sqlData = sql.getDataVerify().createSql(parameters);
		if(!sqlData.isResult()) {
			checkVO.setResult(CheckVO.CHECK_FAILED);
			return checkVO;
		}
		checkVO.setSqlData(sqlData);
		checkVO.setResult(CheckVO.SUCCESS);
		return checkVO;
	}
}
